package com.kgc.member.memberweb.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.member.bean.User;
import com.kgc.member.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
public class UserController {
    @Reference
    UserService userService;

    @RequestMapping("/dologin")
    @ResponseBody
    public Map<String, Object> dologin(@RequestParam(value = "name",required = false,defaultValue = "") String name,
                                       @RequestParam(value = "password",required = false,defaultValue = "") String password){
        Map<String, Object> dologinMap = userService.doLogin(name,password);
        return dologinMap;
    }

    User users;
    @RequestMapping("/tomain")
    public String tomain(Integer id, Model model){
        users = userService.selectById(id);
        model.addAttribute("user",users);
        return "main";
    }
    @RequestMapping("/tuichu")
    public String tuichu(){
        return "redirect:/tologin";
    }

    @RequestMapping("/tologin")
    public String tologin(){
        return "login";
    }
    @RequestMapping("/seleUsers")
    @ResponseBody
    public PageInfo<User> seleUsers(@RequestParam(value = "name",required = false,defaultValue = "")String name,
                              @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum){
        PageInfo<User> pageInfo = userService.selectUser(name, pageNum);
        return pageInfo;
    }
    @RequestMapping("/seleusersByPassword")
    @ResponseBody
    public String seleusersByPassword(String password){
        String users = userService.getUser(password);
        return users;
    }

    @RequestMapping("/doup")
    @ResponseBody
    public String doup(String password){
        users.setPassword(password);
        Integer integer = userService.updateUser(users);
        if(integer>0){
            return "SUCCESS";
        }else{
            return "FAIL";
        }
    }

}
